/*
 +-----------------------------------------------------------------------------------------------+
 | Project : UKAPI
 +-----------------------------------------------------------------------------------------------+
 | Project Description : Universal & Kernel API
 +-----------------------------------------------------------------------------------------------+
 | File Description :
 |   Error Check
 |                                                 
 +-----------------------------------------------------------------------------------------------+
 | Licence : MIT Licence
 +-----------------------------------------------------------------------------------------------+
 | Copyright (c) 2006 Olivier Vanhoucke
 | 
 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 | and associated documentation files (the "Software"), to deal in the Software without 
 | restriction, including without limitation the rights to use, copy, modify, merge, publish, 
 | distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 
 | Software is furnished to do so, subject to the following conditions:
 | 
 | The above copyright notice and this permission notice shall be included in all copies or 
 | substantial portions of the Software.
 | 
 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 +-----------------------------------------------------------------------------------------------+
*/

/**
 * Inclusions standards
 */
#include <ukapi.h>

/*
CRC-16 : X16 + X15 + X2 + 1 
*/

const word_t internal_crc16_table[16] = 
{ 
    0x0000, 0xCC01, 0xD801, 0x1400,
    0xF001, 0x3C00, 0x2800, 0xE401,
    0xA001, 0x6C00, 0x7800, 0xB401,
    0x5000, 0x9C01, 0x8801, 0x4400 
};

word_t errorcheck_get_crc16( pbyte_t byte, size_t size )
{
    word_t	crc = 0;
    size_t	i	= 0;
	word_t	r1	= 0;
    
    for ( ; i < size; i++, byte++ ) 
	{
        /* lower 4 bits */
        r1 = internal_crc16_table[crc & 0xF];
        crc = ( crc >> 4 ) & 0x0FFF;
        crc = crc ^ r1 ^ internal_crc16_table[*byte & 0xF];
        
        /* upper 4 bits */
        r1 = internal_crc16_table[crc & 0xF];
        crc = ( crc >> 4 ) & 0x0FFF;
        crc = crc ^ r1 ^ internal_crc16_table[(*byte >> 4) & 0xF];
    }

    return crc;
}
